#!/usr/bin/perl
#edit_folder.cgi

require './folderman-lib.pl';

&ReadParse();

my $pdir = $in{'locate'};
my $emode = $in{'edit'};
my $folder = $in{'dir'};
if (!(-e $pdir))
{
	&nas_syslog("folderman", "naslog_warning", 30013, "Directory invalid!");
	&error($text{'dir_invalid'});
}
if ($emode=~ /create/)
{
	&header($text{'create_folder_title'}, "", "createfolder", 0, 1);
#	print "<hr>\n";
	print "<form action=save_folder.cgi method=post onsubmit='document.forms[0].savefolder.disabled = true;'>\n";
	print "<input type=hidden name=location value='$pdir'>\n";
	print "<input type=hidden name=edit value='create'>\n";
	print "<table Width=100% class=group border=1 cellpadding=5 cellspacing=0>\n";
	print "<tr class=title1> <td class=group><b>$text{'create_folder'}</b></td> </tr>\n";
	print "<tr> <td class=group><table class=mode1>\n";
	print "<tr> <td><b>$text{'foldername'}</b></td>\n";
	print "<td><input name=foldername class=text size=30></td> </tr>\n";
	print "<tr> <td><b>$text{'location'}</b></td>\n";
	print "<td><input value='$pdir' class=disabletext disabled=true size=30></td> </tr>\n";
	print "<tr> <td><b>$text{'attribute'}</b></td>";
	print "<td><input type=checkbox name=attr value='r' checked>$text{'readable'}";
	print "<input type=checkbox name=attr value='w' checked>$text{'writable'}";
	print "<input type=checkbox name=attr value='e' checked>$text{'executable'}";
	print "<input type=checkbox name=attr value='t' checked>$text{'undelete'}</td></tr>";
  print "<tr> <td colspan=2>\n";
	print "<hr>";
	print "<tr><td><input type=submit name=savefolder value='$text{'submit'}' class=button1></td>\n";
	print "</table></td></tr></table></form>\n";
}
if ($emode=~ /property/)
{
	&header($text{'folder_property_title'}, "", "editfolder", 0, 1);
#	print "<hr>\n";
	if ($folder=~ /.+/)
	{
		print "<form action=save_folder.cgi method=post onsubmit='document.forms[0].savefolder.disabled = true;'>\n";
		print "<input type=hidden name=location value='$pdir'>\n";
		print "<input type=hidden name=edit value='property'>\n";
		print "<table width=100% class=group border=1 cellpadding=4 cellspacing=0>\n";
		print "<tr class=title1> <td class=group><b>$text{'folder_property'}</b></td> </tr>\n";
		print "<tr> <td class=group><table class=mode1>\n";
		print "<tr> <td><b>$text{'foldername'}</b></td>\n";
		my @st = stat($folder);
		$folder =~ s/^$pdir//;
		print "<td><input name=foldername value='$folder' class=text size=30></td> </tr>\n";
		print "<input type=hidden name=oldname value='$folder'>\n";
		print "<tr> <td><b>$text{'location'}</b></td>\n";
		print "<td><input value='$pdir' class=disabletext disabled=true size=30></td> </tr>\n";
		print "<tr> <td><b>$text{'attribute'}</b></td>\n";
		if ($st[2] & 0004)
		{
			print "<td><input type=checkbox name=attr value='r' checked>$text{'readable'}"
		}
		else
		{
			print "<td><input type=checkbox name=attr value='r'>$text{'readable'}";
		}
		if ($st[2] & 0002)
		{
			print "<input type=checkbox name=attr value='w' checked>$text{'writable'}"
		}
		else
		{
			print "<input type=checkbox name=attr value='w'>$text{'writable'}";
		}
		if ($st[2] & 0001)
		{
			print "<input type=checkbox name=attr value='e' checked>$text{'executable'}"
		}
		else
		{
			print "<input type=checkbox name=attr value='e'>$text{'executable'}";
		}
		#niu for acl
		if ($st[2] & 01000)
		{
			print "<input type=checkbox name=attr value='t' checked>$text{'undelete'}</td> </tr>\n"
		}
		else
		{
			print "<input type=checkbox name=attr value='t'>$text{'undelete'}</td> </tr>\n";
		}
		
    print "<tr><td> <input type=checkbox name=subdir value='s'></td><td>$text{'workonsubdir'}</td></tr>\n";
		print "<tr> <td colspan=2>\n";
		print "<hr>";
		print "<input type=submit name=savefolder value='$text{'submit'}'  class=button1>\n";

		print "</table></td></tr></table></form>\n";
	}
	else
	{
		print "<p>", $text{'noselectfolder'}, "</p>\n";
	}
}

#niu for acl


print <<EOF;
<script>
function change_user_list()
{
	RefreshPage();
}
function RefreshPage()
{
	document.forms[0].action = 'edit_folder.cgi';
	#document.forms[0].submit();
}
function addcifstrustlist(n)
{
	var idx = document.forms[0].userslist.selectedIndex;
        var i, find;
        var uitem;

        find = 0;
	if (idx < 0)
	{
		alert("$text{'noselectcifsuser'}");
		return;
	}
	document.forms[0].trustusercache.value = "";
	if (n > 1)
	{
		for (i = 0; i < document.forms[0].cifstrustrdn.length; i++)
		{
			document.forms[0].trustusercache.value +=(document.forms[0].cifsuseritem[i].value +","+
							document.forms[0].cifsaccessitem[i].value + ":");
                        if(!find)
                        {
                                 uitem = (document.forms[0].usertyperdn.value +","+ document.forms[0].userslist.value);
                                 if(document.forms[0].cifsuseritem[i].value == uitem)
                                 {
                                         find = 1;
                                 }
                        }
		}
	}
	else if (n == 1)
	{
		document.forms[0].trustusercache.value +=(document.forms[0].cifsuseritem.value +","+
							document.forms[0].cifsaccessitem.value + ":");
                if(!find)
                {
                        uitem = (document.forms[0].usertyperdn.value +","+ document.forms[0].userslist.value);
                        if(document.forms[0].cifsuseritem.value == uitem)
                        {
                                 find = 1;
                        }
                }
	}

        if(find == 1)
        {
                alert("$text{'msg_item_exist'}");
                return false;
        }

	document.forms[0].trustusercache.value +=(document.forms[0].usertyperdn.value +","+
							document.forms[0].userslist.value +"," +
							document.forms[0].cifsaccesssel.value);


	RefreshPage();
}

function removecifstrustlist(n)
{
	var found = -1;
	if (n > 1)
	{
		for (i = 0; i < document.forms[0].cifstrustrdn.length; i++)
		{
			if (document.forms[0].cifstrustrdn[i].checked)
			{
				found = i;
				break;
			}
		}
	}
	else if (n == 1)
	{
		if (document.forms[0].cifstrustrdn.checked)
		{
			found = 0;
		}
	}
	if (found >= 0)
	{
		document.forms[0].trustusercache.value = "";
		if (n > 1)
		{
			for (i = 0; i < document.forms[0].cifstrustrdn.length; i++)
			{
				if (i != found)
					document.forms[0].trustusercache.value +=(document.forms[0].cifsuseritem[i].value +","+
							document.forms[0].cifsaccessitem[i].value + ":");
			}
		}
		RefreshPage();
	}
	else
	{
		alert("$text{'noselecttrustuser'}");
	}
}

function cachecifslist(n)
{
	document.forms[0].trustusercache.value = "";
	if (n > 1)
	{
		for (i = 0; i < document.forms[0].cifstrustrdn.length; i++)
		{
			document.forms[0].trustusercache.value +=(document.forms[0].cifsuseritem[i].value +","+
							document.forms[0].cifsaccessitem[i].value);
			if (i < (document.forms[0].cifstrustrdn.length - 1))
				document.forms[0].trustusercache.value += ":";
		}
	}
	else if (n == 1)
	{
		document.forms[0].trustusercache.value +=(document.forms[0].cifsuseritem.value +","+
							document.forms[0].cifsaccessitem.value);
	}
}

function addtoaccesslist(n)
{
	document.forms[0].restrictlistcache.value = "";
	if (n > 1)
	{
		for (i = 0; i < document.forms[0].accessaction.length; i++)
		{
			document.forms[0].restrictlistcache.value +=(document.forms[0].accessaction[i].value +","+
							document.forms[0].accesscondition[i].value +"," +
							document.forms[0].accessin[i].value + ":");
		}
	}
	else if (n == 1)
	{
		document.forms[0].restrictlistcache.value +=(document.forms[0].accessaction.value +","+
							document.forms[0].accesscondition.value +"," +
							document.forms[0].accessin.value + ":");
	}
	document.forms[0].restrictlistcache.value +=(document.forms[0].actionadd.value +","+
							document.forms[0].conditionadd.value +"," +
							document.forms[0].conditionaddin.value);
	document.forms[0].actionadd.value = 0;
	document.forms[0].conditionadd.value = 0;
	document.forms[0].conditionaddin.value = "";
	RefreshPage();
}

function removefromaccesslist(n)
{
	var found = -1;
	if (n > 1)
	{
		for (i = 0; i < document.forms[0].accessaction.length; i++)
		{
			if (document.forms[0].selectitem[i].checked)
			{
				found = i;
				break;
			}
		}
	}
	else if (n == 1)
	{
		if (document.forms[0].selectitem.checked)
		{
			found = 0;
		}
	}
	if (found >= 0)
	{
		document.forms[0].restrictlistcache.value = "";
		if (n > 1)
		{
			for (i = 0; i < document.forms[0].accessaction.length; i++)
			{
				if (i != found)
					document.forms[0].restrictlistcache.value +=(document.forms[0].accessaction[i].value +","+
							document.forms[0].accesscondition[i].value +"," +
							document.forms[0].accessin[i].value + ":");
			}
		}
		RefreshPage();
	}
	else
	{
		alert("$text{'noselectaccess'}");
	}
}

function cacheaccesslist(n)
{
	document.forms[0].restrictlistcache.value = "";
	if (n > 1)
	{
		for (i = 0; i < document.forms[0].accessaction.length; i++)
		{
			document.forms[0].restrictlistcache.value +=(document.forms[0].accessaction[i].value +","+
							document.forms[0].accesscondition[i].value +"," +
							document.forms[0].accessin[i].value);
			if (i < (document.forms[0].accessaction.length - 1))
				document.forms[0].restrictlistcache.value += ":";
		}
	}
	else if (n == 1)
	{
		document.forms[0].restrictlistcache.value +=(document.forms[0].accessaction.value +","+
							document.forms[0].accesscondition.value +"," +
							document.forms[0].accessin.value);
	}
}

function checkselected(obj)
{
	if (!document.forms[0].allowanony.checked&&!document.forms[0].allowgroup.checked)
	{
		alert("$text{'must_select_one'}");
		obj.checked = true;
	}
}
</script>
EOF
#niu for acl end

#print "<hr>\n";

&footer("index.cgi?dir=$pdir",  $text{'list_folders_return'});